#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <math.h>
#include <windows.h>

// 控制台颜色设置
void SetColor(int ForgC) {
    WORD wColor;
    HANDLE hStdOut = GetStdHandle(STD_OUTPUT_HANDLE);
    CONSOLE_SCREEN_BUFFER_INFO csbi;
    
    if(GetConsoleScreenBufferInfo(hStdOut, &csbi)) {
        wColor = (csbi.wAttributes & 0xF0) + (ForgC & 0x0F);
        SetConsoleTextAttribute(hStdOut, wColor);
    }
}

// 获取当前时间
void GetCurTime(char* timeStr) {
    time_t now = time(NULL);
    struct tm* tm_now = localtime(&now);
    strftime(timeStr, 20, "%Y-%m-%d %H:%M:%S", tm_now);
}

// 检查是否为闰年
bool IsLeapYear(int year) {
    return (year % 4 == 0 && year % 100 != 0) || (year % 400 == 0);
}

// 获取某个月的天数
int GetMonthDays(int year, int month) {
    if (month == 2) {
        return IsLeapYear(year) ? 29 : 28;
    } else if (month == 4 || month == 6 || month == 9 || month == 11) {
        return 30;
    } else {
        return 31;
    }
}

// 验证日期是否有效
bool IsValidDate(int year, int month, int day) {
    if (year < 1900 || year > 2100) return false;
    if (month < 1 || month > 12) return false;
    if (day < 1 || day > GetMonthDays(year, month)) return false;
    return true;
}

// 验证农历日期是否有效
bool IsValidLunarDate(int year, int month, int day, bool isLeapMonth) {
    if (year < 1900 || year > 2100) return false;
    if (month < 1 || month > 12) return false;
    if (day < 1 || day > 30) return false;
    // 这里应该有更精确的农历月份天数验证
    return true;
}

// 计算两个日期之间的天数差
int DateDiff(int y1, int m1, int d1, int y2, int m2, int d2) {
    struct tm tm1 = {0, 0, 0, d1, m1 - 1, y1 - 1900, 0, 0, -1};
    struct tm tm2 = {0, 0, 0, d2, m2 - 1, y2 - 1900, 0, 0, -1};
    
    time_t time1 = mktime(&tm1);
    time_t time2 = mktime(&tm2);
    
    if (time1 == -1 || time2 == -1) {
        return -1;
    }
    
    return (int)difftime(time2, time1) / (60 * 60 * 24);
}

// ==========农历计算部分 

// 农历数据（1900-2100年，每年对应的农历信息）
unsigned int LunarInfo[] = {
    0x04bd8,0x04ae0,0x0a570,0x054d5,0x0d260,0x0d950,0x16554,0x056a0,0x09ad0,0x055d2, //1900-1909
    0x04ae0,0x0a5b6,0x0a4d0,0x0d250,0x1d255,0x0b540,0x0d6a0,0x0ada2,0x095b0,0x14977, //1910-1919
    0x04970,0x0a4b0,0x0b4b5,0x06a50,0x06d40,0x1ab54,0x02b60,0x09570,0x052f2,0x04970, //1920-1929
    0x06566,0x0d4a0,0x0ea50,0x06e95,0x05ad0,0x02b60,0x186e3,0x092e0,0x1c8d7,0x0c950, //1930-1939
    0x0d4a0,0x1d8a6,0x0b550,0x056a0,0x1a5b4,0x025d0,0x092d0,0x0d2b2,0x0a950,0x0b557, //1940-1949
    0x06ca0,0x0b550,0x15355,0x04da0,0x0a5d0,0x14573,0x052d0,0x0a9a8,0x0e950,0x06aa0, //1950-1959
    0x0aea6,0x0ab50,0x04b60,0x0aae4,0x0a570,0x05260,0x0f263,0x0d950,0x05b57,0x056a0, //1960-1969
    0x096d0,0x04dd5,0x04ad0,0x0a4d0,0x0d4d4,0x0d250,0x0d558,0x0b540,0x0b5a0,0x195a6, //1970-1979
    0x095b0,0x049b0,0x0a974,0x0a4b0,0x0b27a,0x06a50,0x06d40,0x0af46,0x0ab60,0x09570, //1980-1989
    0x04af5,0x04970,0x064b0,0x074a3,0x0ea50,0x06b58,0x055c0,0x0ab60,0x096d5,0x092e0, //1990-1999
    0x0c960,0x0d954,0x0d4a0,0x0da50,0x07552,0x056a0,0x0abb7,0x025d0,0x092d0,0x0cab5, //2000-2009
    0x0a950,0x0b4a0,0x0baa4,0x0ad50,0x055d9,0x04ba0,0x0a5b0,0x15176,0x052b0,0x0a930, //2010-2019
    0x07954,0x06aa0,0x0ad50,0x05b52,0x04b60,0x0a6e6,0x0a4e0,0x0d260,0x0ea65,0x0d530, //2020-2029
    0x05aa0,0x076a3,0x096d0,0x04bd7,0x04ad0,0x0a4d0,0x1d0b6,0x0d250,0x0d520,0x0dd45, //2030-2039
    0x0b5a0,0x056d0,0x055b2,0x049b0,0x0a577,0x0a4b0,0x0aa50,0x1b255,0x06d20,0x0ada0, //2040-2049
    0x14b63,0x09370,0x049f8,0x04970,0x064b0,0x168a6,0x0ea50,0x06b20,0x1a6c4,0x0aae0, //2050-2059
    0x092e0,0x0d2e3,0x0c960,0x0d557,0x0d4a0,0x0da50,0x05d55,0x056a0,0x0a6d0,0x055d4, //2060-2069
    0x052d0,0x0a9b8,0x0a950,0x0b4a0,0x0b6a6,0x0ad50,0x055a0,0x0aba4,0x0a5b0,0x052b0, //2070-2079
    0x0b273,0x06930,0x07337,0x06aa0,0x0ad50,0x14b55,0x04b60,0x0a570,0x054e4,0x0d160, //2080-2089
    0x0e968,0x0d520,0x0daa0,0x16aa6,0x056d0,0x04ae0,0x0a9d4,0x0a2d0,0x0d150,0x0f252, //2090-2099
    0x0d520 //2100
};

// 农历月份名称
const char* LunarMonthNames[] = {
    "正月", "二月", "三月", "四月", "五月", "六月", 
    "七月", "八月", "九月", "十月", "冬月", "腊月"
};

// 农历日名称
const char* LunarDayNames[] = {
    "初一", "初二", "初三", "初四", "初五", "初六", "初七", "初八", "初九", "初十",
    "十一", "十二", "十三", "十四", "十五", "十六", "十七", "十八", "十九", "二十",
    "廿一", "廿二", "廿三", "廿四", "廿五", "廿六", "廿七", "廿八", "廿九", "三十"
};

// 天干
const char* Gan[] = {"甲", "乙", "丙", "丁", "戊", "己", "庚", "辛", "壬", "癸"};

// 地支
const char* Zhi[] = {"子", "丑", "寅", "卯", "辰", "巳", "午", "未", "申", "酉", "戌", "亥"};

// 生肖
const char* Animals[] = {"鼠", "牛", "虎", "兔", "龙", "蛇", "马", "羊", "猴", "鸡", "狗", "猪"};

// 节气名称
const char* SolarTerms[] = {
    "小寒", "大寒", "立春", "雨水", "惊蛰", "春分", 
    "清明", "谷雨", "立夏", "小满", "芒种", "夏至", 
    "小暑", "大暑", "立秋", "处暑", "白露", "秋分", 
    "寒露", "霜降", "立冬", "小雪", "大雪", "冬至"
};

// 节气日期计算
void CalculateSolarTerms(int year, int month, int day, char* termName, int* nextTermDays) {
    static int termDates[24] = {
        5, 20, 4, 18, 5, 20, 4, 20, 5, 21, 5, 21, 
        7, 22, 7, 23, 7, 23, 8, 23, 7, 22, 7, 21
    };
    
    int termMonth = month;
    int termDay = termDates[(month-1)*2];
    
    if (day < termDay) {
        strcpy(termName, SolarTerms[(month-1)*2]);
        *nextTermDays = termDay - day;
    } else {
        termDay = termDates[(month-1)*2+1];
        if (day < termDay) {
            strcpy(termName, SolarTerms[(month-1)*2+1]);
            *nextTermDays = termDay - day;
        } else {
            termMonth = month % 12 + 1;
            termDay = termDates[(termMonth-1)*2];
            strcpy(termName, SolarTerms[(termMonth-1)*2]);
            *nextTermDays = GetMonthDays(year, month) - day + termDay;
        }
    }
}

// 获取农历月份的天数
int GetLunarMonthDays(int lunarYear, int lunarMonth) {
    if ((LunarInfo[lunarYear - 1900] & (0x10000 >> lunarMonth))) {
        return 30;
    } else {
        return 29;
    }
}

// 获取农历闰月月份
int GetLunarLeapMonth(int lunarYear) {
    return LunarInfo[lunarYear - 1900] & 0xf;
}

// 获取农历闰月的天数
int GetLunarLeapDays(int lunarYear) {
    if (GetLunarLeapMonth(lunarYear)) {
        if ((LunarInfo[lunarYear - 1900] & 0x10000)) {
            return 30;
        } else {
            return 29;
        }
    }
    return 0;
}

// 获取农历年的总天数
int GetLunarYearDays(int lunarYear) {
    int sum = 348; // 29天*12个月
    for (int i = 0x8000; i > 0x8; i >>= 1) {
        if ((LunarInfo[lunarYear - 1900] & i)) {
            sum++;
        }
    }
    return sum + GetLunarLeapDays(lunarYear);
}	

// 公历转农历
void SolarToLunar(int year, int month, int day, int* lyear, int* lmonth, int* lday, bool* isLeapMonth) {
    
    if (!IsValidDate(year, month, day)) {
        *lyear = *lmonth = *lday = 0;
        *isLeapMonth = false;
        return;
    }
    
    *lyear = year;
    *lmonth = month;
    *lday = day;
    *isLeapMonth = false;
}

// ================农历转公历 

// 农历转公历
void LunarToSolar(int lunarYear, int lunarMonth, int lunarDay, bool isLeapMonth, 
                 int* solarYear, int* solarMonth, int* solarDay) {
    // 检查输入有效性
    if (!IsValidLunarDate(lunarYear, lunarMonth, lunarDay, isLeapMonth)) {
        *solarYear = *solarMonth = *solarDay = 0;
        return;
    }
    
    // 计算农历年的总天数
    int days = 0;
    for (int y = 1900; y < lunarYear; y++) {
        days += GetLunarYearDays(y);
    }
    
    // 计算农历月的天数
    int leapMonth = GetLunarLeapMonth(lunarYear);
    bool isLeap = false;
    
    for (int m = 1; m < lunarMonth; m++) {
        if (leapMonth > 0 && m == leapMonth && !isLeap) {
            // 处理闰月
            days += GetLunarLeapDays(lunarYear);
            isLeap = true;
            m--; // 需要再处理这个月
            leapMonth = 0; // 已经处理过闰月
        } else {
            days += GetLunarMonthDays(lunarYear, m);
        }
    }
    
    // 处理闰月
    if (isLeapMonth && lunarMonth == leapMonth) {
        days += GetLunarMonthDays(lunarYear, lunarMonth);
    }
    
    // 加上当前月的天数
    days += lunarDay;
    
    // 转换为公历日期
    *solarYear = 1900;
    *solarMonth = 1;
    *solarDay = 1;
    
    // 从1900年1月1日开始累加天数
    while (days > 0) {
        int monthDays = GetMonthDays(*solarYear, *solarMonth);
        
        if (days > monthDays) {
            days -= monthDays;
            (*solarMonth)++;
            
            if (*solarMonth > 12) {
                *solarMonth = 1;
                (*solarYear)++;
            }
        } else {
            *solarDay = days;
            days = 0;
        }
    }
}

// 获取天干地支年
void GetGanZhiYear(int year, char* ganZhi) {
    int ganIndex = (year - 4) % 10;
    int zhiIndex = (year - 4) % 12;
    sprintf(ganZhi, "%s%s", Gan[ganIndex], Zhi[zhiIndex]);
}

// 获取生肖
const char* GetAnimalYear(int year) {
    return Animals[(year - 4) % 12];
}

// ================ 月历显示 ================

// 显示月历（包含农历和节气）
void ShowMonthCalendarWithLunar(int year, int month) {
    if (!IsValidDate(year, month, 1)) {
        printf("无效的年月输入!\n");
        return;
    }
    
    struct tm tm = {0, 0, 0, 1, month - 1, year - 1900, 0, 0, -1};
    mktime(&tm);
    
    int firstDayOfWeek = tm.tm_wday; // 该月第一天是星期几 (0-6, 0=周日)
    int daysInMonth = GetMonthDays(year, month);
    
    printf("\n      %d年 %d月\n", year, month);
    printf("日 一 二 三 四 五 六\n");
    
    // 打印前面的空格
    for (int i = 0; i < firstDayOfWeek; i++) {
        printf("   ");
    }
    
    // 打印日期
    for (int day = 1; day <= daysInMonth; day++) {
        // 检查是否是节气
        char termName[20] = {0};
        int nextTermDays = 0;
        CalculateSolarTerms(year, month, day, termName, &nextTermDays);
        
        bool isTerm = (strlen(termName) > 0 && nextTermDays == 0);
        bool isWeekend = (firstDayOfWeek + day - 1) % 7 == 0 || (firstDayOfWeek + day - 1) % 7 == 6;
        
        // 获取农历日期
        int lyear, lmonth, lday;
        bool isLeapMonth;
        SolarToLunar(year, month, day, &lyear, &lmonth, &lday, &isLeapMonth);
        
        // 设置颜色
        if (isTerm) {
            SetColor(FOREGROUND_RED | FOREGROUND_INTENSITY);
        } else if (isWeekend) {
            SetColor(FOREGROUND_RED);
        } else {
            SetColor(FOREGROUND_BLUE);
        }
        
        printf("%2d", day);
        
        // 恢复默认颜色
        SetColor(FOREGROUND_BLUE | FOREGROUND_GREEN | FOREGROUND_RED);
        
        // 打印农历和节气
        if (isTerm) {
            printf("*"); // 节气标记
        } else {
            printf(" ");
        }
        
        // 换行
        if ((firstDayOfWeek + day) % 7 == 0) {
            printf("\n");
        }
    }
    
    printf("\n\n");
    printf("注: *表示节气\n");
    
    // 显示当前月的节气
    printf("\n本月节气: ");
    char term1[20], term2[20];
    int dummy;
    CalculateSolarTerms(year, month, 1, term1, &dummy);
    CalculateSolarTerms(year, month, 15, term2, &dummy);
    printf("%s, %s\n", term1, term2);
}

// ================ 功能函数 ================

// 查询农历日期
void QueryLunarDate() {
    int year, month, day;
    printf("请输入公历日期(年 月 日, 如2020 10 20): ");
    if (scanf("%d %d %d", &year, &month, &day) != 3 || !IsValidDate(year, month, day)) {
        printf("无效的日期输入!\n");
        while (getchar() != '\n'); // 清空输入缓冲区
        return;
    }
    
    // 转换为农历
    int lyear, lmonth, lday;
    bool isLeapMonth;
    SolarToLunar(year, month, day, &lyear, &lmonth, &lday, &isLeapMonth);
    
    if (lyear == 0) {
        printf("日期转换失败!\n");
        return;
    }
    
    // 计算天干地支
    char ganZhi[10];
    GetGanZhiYear(lyear, ganZhi);
    const char* animal = GetAnimalYear(lyear);
    
    // 检查节气
    char termName[20] = {0};
    int nextTermDays = 0;
    CalculateSolarTerms(year, month, day, termName, &nextTermDays);
    
    printf("\n查询结果:\n");
    printf("1. 公历日期: %d年%d月%d日\n", year, month, day);
    printf("2. 农历日期: %d年%s%s%s\n", lyear, isLeapMonth?"闰":"", LunarMonthNames[lmonth-1], LunarDayNames[lday-1]);
    printf("3. 天干地支: %s(%s)年\n", ganZhi, animal);
    
    if (strlen(termName) > 0 && nextTermDays == 0) {
        printf("4. 节气信息: 今天是【%s】\n", termName);
    } else {
        printf("4. 节气信息: 距离下个节气【%s】还有%d天\n", termName, nextTermDays);
    }
    
    // 显示当月节气
    printf("\n当月节气: ");
    char term1[20], term2[20];
    int dummy;
    CalculateSolarTerms(year, month, 1, term1, &dummy);
    CalculateSolarTerms(year, month, 15, term2, &dummy);
    printf("%s, %s\n", term1, term2);
    
    ShowMonthCalendarWithLunar(year, month);
}

// 查询公历日期
void QueryGregorianDate() {
    int year, month, day;
    char leap[10] = {0};
    printf("请输入农历日期(年 月 日 是否闰月[0/1], 如2021 11 18 0): ");
    if (scanf("%d %d %d %s", &year, &month, &day, leap) != 4) {
        printf("无效的输入格式!\n");
        while (getchar() != '\n'); // 清空输入缓冲区
        return;
    }
    
    bool isLeapMonth = (strcmp(leap, "1") == 0);
    
    if (!IsValidLunarDate(year, month, day, isLeapMonth)) {
        printf("无效的农历日期!\n");
        return;
    }
    
    int solarYear, solarMonth, solarDay;
    LunarToSolar(year, month, day, isLeapMonth, &solarYear, &solarMonth, &solarDay);
    
    if (solarYear == 0) {
        printf("\n农历日期转换失败!\n");
    } else {
        printf("\n农历 %d年%s%d月%d日 对应的公历日期是: %d年%d月%d日\n", 
               year, isLeapMonth ? "闰" : "", month, day, 
               solarYear, solarMonth, solarDay);
        
        // 计算星期几
        struct tm tm = {0, 0, 0, solarDay, solarMonth - 1, solarYear - 1900, 0, 0, -1};
        mktime(&tm);
        const char* weekdays[] = {"日", "一", "二", "三", "四", "五", "六"};
        printf("星期%s\n", weekdays[tm.tm_wday]);
    }
}

// 显示月历
void DisplayMonthCalendar() {
    int year, month;
    printf("请输入要查询的年月(年 月, 如2021 11): ");
    if (scanf("%d %d", &year, &month) != 2 || !IsValidDate(year, month, 1)) {
        printf("无效的年月输入!\n");
        while (getchar() != '\n'); // 清空输入缓冲区
        return;
    }
    
    ShowMonthCalendarWithLunar(year, month);
}

// 查询某天距今天的天数
void DaysFromToday() {
    int year, month, day;
    printf("请输入日期(年 月 日, 如2020 11 3): ");
    if (scanf("%d %d %d", &year, &month, &day) != 3 || !IsValidDate(year, month, day)) {
        printf("无效的日期输入!\n");
        while (getchar() != '\n'); // 清空输入缓冲区
        return;
    }
    
    time_t now = time(NULL);
    struct tm* tm_now = localtime(&now);
    int currentYear = tm_now->tm_year + 1900;
    int currentMonth = tm_now->tm_mon + 1;
    int currentDay = tm_now->tm_mday;
    
    int diff = DateDiff(year, month, day, currentYear, currentMonth, currentDay);
    
    if (diff == -1) {
        printf("日期计算错误!\n");
    } else if (diff > 0) {
        printf("%d年%d月%d日比今天(%d-%d-%d)早【%d】天\n", 
               year, month, day, currentYear, currentMonth, currentDay, diff);
    } else if (diff < 0) {
        printf("%d年%d月%d日比今天(%d-%d-%d)晚【%d】天\n", 
               year, month, day, currentYear, currentMonth, currentDay, -diff);
    } else {
        printf("输入的日期就是今天!\n");
    }
}

// 查询距今天相应天数的日期
void DateFromDays() {
    int days;
    printf("请输入天数(如365): ");
    if (scanf("%d", &days) != 1) {
        printf("无效的天数输入!\n");
        while (getchar() != '\n'); // 清空输入缓冲区
        return;
    }
    
    time_t now = time(NULL);
    struct tm tm_now = *localtime(&now);
    
    // 计算之后的日期
    time_t future = now + days * 24 * 60 * 60;
    struct tm tm_future = *localtime(&future);
    
    // 计算之前的日期
    time_t past = now - days * 24 * 60 * 60;
    struct tm tm_past = *localtime(&past);
    
    printf("距离今天(%d-%d-%d)【%d】天的日期为:\n", 
           tm_now.tm_year + 1900, tm_now.tm_mon + 1, tm_now.tm_mday, days);
    printf("之后的日期: %d年%d月%d日\n", 
           tm_future.tm_year + 1900, tm_future.tm_mon + 1, tm_future.tm_mday);
    printf("之前的日期: %d年%d月%d日\n", 
           tm_past.tm_year + 1900, tm_past.tm_mon + 1, tm_past.tm_mday);
}

// 查询任意两天之间的天数差
void DaysBetweenDates() {
    int y1, m1, d1, y2, m2, d2;
    printf("请输入第一个日期(年 月 日): ");
    if (scanf("%d %d %d", &y1, &m1, &d1) != 3 || !IsValidDate(y1, m1, d1)) {
        printf("无效的第一个日期输入!\n");
        while (getchar() != '\n'); // 清空输入缓冲区
        return;
    }
    
    printf("请输入第二个日期(年 月 日): ");
    if (scanf("%d %d %d", &y2, &m2, &d2) != 3 || !IsValidDate(y2, m2, d2)) {
        printf("无效的第二个日期输入!\n");
        while (getchar() != '\n'); // 清空输入缓冲区
        return;
    }
    
    int diff = DateDiff(y1, m1, d1, y2, m2, d2);
    if (diff == -1) {
        printf("日期计算错误!\n");
    } else if (diff >= 0) {
        printf("%d年%d月%d日 比 %d年%d月%d日 晚 %d 天\n", 
               y2, m2, d2, y1, m1, d1, diff);
    } else {
        printf("%d年%d月%d日 比 %d年%d月%d日 早 %d 天\n", 
               y2, m2, d2, y1, m1, d1, -diff);
    }
}

// 显示二十四节气
void ShowSolarTerms() {
    int year;
    printf("请输入要查询的年份(如2016): ");
    if (scanf("%d", &year) != 1 || year < 1900 || year > 2100) {
        printf("无效的年份输入! (1900-2100)\n");
        while (getchar() != '\n'); // 清空输入缓冲区
        return;
    }
    
    printf("\n%d年的二十四节气:\n", year);
    printf("========================\n");
    
    for (int i = 0; i < 24; i += 2) {
        printf("%-6s: %d月%d日\t", SolarTerms[i], (i/2)+1, 5);
        printf("%-6s: %d月%d日\n", SolarTerms[i+1], (i/2)+1, 20);
    }
}

// 显示节日
void ShowFestivals() {
    printf("\n1. 公历节日\n");
    printf("2. 农历节日\n");
    printf("请选择: ");
    
    int choice;
    if (scanf("%d", &choice) != 1 || (choice != 1 && choice != 2)) {
        printf("无效选择!\n");
        while (getchar() != '\n');
        return;
    }

    // 公历节日数据结构
    struct SolarFestival {
        int month;
        int day;
        const char* name;
    };

    // 农历节日数据结构
    struct LunarFestival {
        int month;
        int day;
        const char* name;
    };

    if (choice == 1) {
        // 公历节日数据 (按月份分组)
        SolarFestival festivals[] = {
            {1, 1, "元旦"},        {2, 14, "情人节"},    {3, 8, "妇女节"},
            {3, 12, "植树节"},     {4, 5, "清明节"},     {5, 1, "劳动节"},
            {5, 4, "青年节"},      {6, 1, "儿童节"},     {7, 1, "建党节"},
            {8, 1, "建军节"},      {9, 10, "教师节"},    {10, 1, "国庆节"},
            {10, 31, "万圣节"},    {12, 24, "平安夜"},   {12, 25, "圣诞节"}
        };

        int month;
        printf("请输入要查询的月份(1-12): ");
        if (scanf("%d", &month) != 1 || month < 1 || month > 12) {
            printf("无效的月份输入!\n");
            while (getchar() != '\n');
            return;
        }

        printf("\n%d月的公历节日:\n", month);
        for (size_t i = 0; i < sizeof(festivals)/sizeof(festivals[0]); i++) {
            if (festivals[i].month == month) {
                printf("%2d月%2d日: %s\n", 
                      festivals[i].month, festivals[i].day, festivals[i].name);
            }
        }
    } 
    else if (choice == 2) {
        // 农历节日数据
        LunarFestival festivals[] = {
            {1, 1, "春节"},        {1, 15, "元宵节"},    {2, 2, "龙抬头"},
            {5, 5, "端午节"},      {7, 7, "七夕节"},     {7, 15, "中元节"},
            {8, 15, "中秋节"},     {9, 9, "重阳节"},     {12, 8, "腊八节"},
            {12, 23, "小年"},      {12, 30, "除夕"}
        };

        int month;
        printf("请输入农历月份(1-12): ");
        if (scanf("%d", &month) != 1 || month < 1 || month > 12) {
            printf("无效的月份输入!\n");
            while (getchar() != '\n');
            return;
        }

        printf("\n农历%d月的节日:\n", month);
        for (size_t i = 0; i < sizeof(festivals)/sizeof(festivals[0]); i++) {
            if (festivals[i].month == month) {
                printf("%s: 农历%2d月%2d日\n", 
                      festivals[i].name, festivals[i].month, festivals[i].day);
            }
        }
    }
}

// 主菜单
void MainMenu() {
    char timeStr[20];
    GetCurTime(timeStr);
    
    SetColor(FOREGROUND_RED | FOREGROUND_INTENSITY);
    printf("\n");
    printf("*******************************************\n");
    printf("*          超级万年历                     *\n");
    printf("*          %s            *\n", timeStr);
    printf("*******************************************\n");
    SetColor(FOREGROUND_BLUE | FOREGROUND_INTENSITY);
    
    printf("\n功能菜单:\n");
    printf("1. 查询农历\n");
    printf("2. 查询公历\n");
    printf("3. 显示月历\n");
    printf("4. 查询某天距今天的天数\n");
    printf("5. 查询距今天相应天数的日期\n");
    printf("6. 查询任意两天之间的天数差\n");
    printf("7. 显示二十四节气\n");
    printf("8. 显示节日\n");
    printf("0. 退出系统\n");
    printf("\n请选择功能(0-8): ");
}

int main() {
    
    system("color F0");
    
    int choice;
    do {
        MainMenu();
        if (scanf("%d", &choice) != 1) {
            printf("无效选择，请重新输入!\n");
            while (getchar() != '\n'); // 清空输入缓冲区
            continue;
        }
        
        switch (choice) {
            case 1: QueryLunarDate(); break;
            case 2: QueryGregorianDate(); break;
            case 3: DisplayMonthCalendar(); break;
            case 4: DaysFromToday(); break;
            case 5: DateFromDays(); break;
            case 6: DaysBetweenDates(); break;
            case 7: ShowSolarTerms(); break;
            case 8: ShowFestivals(); break;
            case 0: printf("感谢使用，再见!\n"); break;
            default: printf("无效选择，请重新输入!\n");
        }
        
        if (choice != 0) {
            printf("\n按任意键继续...");
            getchar(); getchar(); // 等待用户按键
        }
    } while (choice != 0);
    
    return 0;
}